package com.maxmlr.concurrent.cyclic;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName TestCyclic
 * @Description TODO
 * @Author maxiao
 * @Date 2020/3/5 上午11:25
 * @Version V1.0
 */
public class TestCyclic {

    public static Map<String,Producer> map=new HashMap<>();
    public static Map<String, CountDownLatch> latchMap=new HashMap<>();

    public static void main(String[] args) {
        int counts = 3;//线程大小
        CyclicBarrier cyclicBarrier = new CyclicBarrier(counts, new MainThread(counts));
        ExecutorService executorService = Executors.newFixedThreadPool(counts);
        executorService.execute(new Producer("111",cyclicBarrier));
        executorService.execute(new Producer("222",cyclicBarrier));
        executorService.execute(new Producer("333",cyclicBarrier));

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("下面模拟接收消费----------------");

        ExecutorService executorService1 = Executors.newFixedThreadPool(counts);
        executorService1.execute(new Consumer("333","000"));
        executorService1.execute(new Consumer("111","000"));
        //executorService1.execute(new Consumer("222","000"));

        executorService.shutdown();
        executorService1.shutdown();
    }

}
